/**********************************************************************
* 文件名：RabbitClient.pro
* 创建日期：2021-06-16 10:14
* 作者：李志勇
* 邮箱：18771909050@163.com
* 版权声明：Copyright (c) 2021 zhiyongli Technology (China) Limited.
* All rights reserved.
* 修改记录:
***********************************************************************/
#include "MqView.h"
#include "ui_MqView.h"

#include "MqController.h"
#include <QTime>

MqView::MqView(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MqView)
{
    ui->setupUi(this);

    Init();
}

MqView::~MqView()
{
    delete ui;
}

void MqView::Init()
{
    if (m_controller)
        return;

    ui->widget_send->setEnabled(false);

    m_controller = std::make_shared<MqController>();
    connect(m_controller.get(), &MqController::signal_connect, this, [&](bool is_connect, const QString& state_msg){
        AppendStateMsg(state_msg);
        ui->btn_connect->setText(is_connect?"断开连接":"连接");
        ui->widget_set->setEnabled(!is_connect);
        ui->widget_send->setEnabled(is_connect);
        if (!is_connect)
        {
            ui->btn_exchange->setEnabled(true);
            ui->btn_queue->setEnabled(true);
        }
    });

    connect(m_controller.get(), &MqController::signal_exchangeDeclared, this, [&](bool is_declared, const QString& state_msg){
        AppendStateMsg(state_msg);
        ui->btn_exchange->setEnabled(!is_declared);
    });

    connect(m_controller.get(), &MqController::signal_queueDeclared, this, [&](bool is_declared, const QString& state_msg){
        AppendStateMsg(state_msg);
        ui->btn_queue->setEnabled(!is_declared);
    });

    connect(m_controller.get(), &MqController::signal_message_received, this, [&](const QString& msg){
        AppendStateMsg(msg);
    });

    InitData();
}

void MqView::InitData()
{
    if (!m_controller)
        return;

    ui->lineEdit_hostName->setText(m_controller->get_host_name());
    ui->spinBox_port->setValue(m_controller->get_port());
    ui->lineEdit_vhost->setText(m_controller->get_virtual_host());
    ui->lineEdit_userName->setText(m_controller->get_user_name());
    ui->lineEdit_password->setText(m_controller->get_password());

    ui->lineEdit_exchangeName->setText(m_controller->get_exchange_name());
    set_exchange_type(static_cast<int>(m_controller->get_exchange_type()));
    ui->checkBox_changeDurable->setChecked(m_controller->get_exchange_durable());
    ui->checkBox_changeAutoDelete->setChecked(m_controller->get_exchange_autoDelete());

    ui->lineEdit_queueName->setText(m_controller->get_queue_name());
    ui->checkBox_queueDurable->setChecked(m_controller->get_queue_durable());
    ui->checkBox_queueExclusive->setChecked(m_controller->get_queue_exclusive());
    ui->checkBox_queueAutoDelete->setChecked(m_controller->get_queue_autoDelete());
}

void MqView::set_exchange_type(int currentIndex)
{
    if (ui->comboBox_exchangeType->count() > currentIndex)
        ui->comboBox_exchangeType->setCurrentIndex(currentIndex);
}

void MqView::AppendStateMsg(const QString &msg)
{
    QString str = QString("[%1]:%2").arg(QTime::currentTime().toString("hh:mm:ss zzz")).arg(msg);
    ui->textEdit_receive->append(str);
}


void MqView::on_btn_connect_clicked()
{
    if (!m_controller)
        return;

    QString msg = ui->btn_connect->text();

    if (msg == "断开连接")
    {
        m_controller->DisConnect();
    }
    else if (msg == "连接")
    {
        m_controller->set_exchange_name(ui->lineEdit_exchangeName->text());
        m_controller->set_queue_name(ui->lineEdit_queueName->text());
        m_controller->CreateConnect();
    }
}

void MqView::on_btn_exchange_clicked()
{
    if (!m_controller)
        return;

    m_controller->CreateExchange();
}

void MqView::on_btn_queue_clicked()
{
    if (!m_controller)
        return;

    m_controller->CreateQueue();
}

void MqView::on_btn_send_clicked()
{
    if (!m_controller)
        return;

    QString msg = ui->lineEdit_send->text();
    QString rountingKey = ui->lineEdit_routingKey->text();
    m_controller->sendMsg(msg, QStringList(rountingKey));
    AppendStateMsg(rountingKey+"发送："+msg);
}

void MqView::on_btn_clear_clicked()
{
    ui->textEdit_receive->clear();
}

void MqView::on_checkBox_changeDurable_clicked(bool checked)
{
    if (m_controller)
        m_controller->set_exchange_durable(checked);
}

void MqView::on_checkBox_changeAutoDelete_clicked(bool checked)
{
    if (m_controller)
        m_controller->set_exchange_autoDelete(checked);
}

void MqView::on_comboBox_exchangeType_currentIndexChanged(int index)
{
    if (m_controller)
        m_controller->set_exchange_type(static_cast<QAmqpExchange::ExchangeType>(index));
}

void MqView::on_checkBox_queueDurable_clicked(bool checked)
{
    if (m_controller)
        m_controller->set_queue_durable(checked);
}

void MqView::on_checkBox_queueExclusive_clicked(bool checked)
{
    if (m_controller)
        m_controller->set_queue_exclusive(checked);
}

void MqView::on_checkBox_queueAutoDelete_clicked(bool checked)
{
    if (m_controller)
        m_controller->set_queue_autoDelete(checked);
}
